package com.jh.user.controller;

import cn.jh.common.exception.ServiceException;
import cn.jh.common.utils.CommonsUtil;
import com.jh.user.business.ShopRegisterBussiness;
import com.jh.user.pojo.ShopRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * @author Yichuan
 * @version 1.0
 */
@RestController
@RequestMapping("/v1.0/user/shop")
public class ShopController {

    private final ShopRegisterBussiness shopRegisterBussiness;

    @Autowired
    public ShopController(ShopRegisterBussiness shopRegisterBussiness) {
        this.shopRegisterBussiness = shopRegisterBussiness;
    }

    @RequestMapping(method = GET, value = ("/platform"))
    public Object getShopByPlatformCode(@RequestParam("platformCode") String platformCode,
                                        @RequestParam("channel") String channel) {
        try {
            ShopRegister shopRegister = shopRegisterBussiness.findShopRegister(platformCode, channel);
            if (shopRegister == null) {
                return CommonsUtil.returnError("商户未进谏");
            }
            ShopDTO dto = ShopDTO.dto(shopRegister);
            return CommonsUtil.returnOK(dto);
        } catch (ServiceException ex) {
            return CommonsUtil.returnError(ex.getMessage());
        }
    }

    @RequestMapping(value = "/shop_register", method = POST)
    public Object findShopRegister(@RequestParam("platformCode") String platformCode) {
        System.out.println("findShopRegister=====>"+platformCode);
        return shopRegisterBussiness.findShopRegister(platformCode);
    }

    @RequestMapping(value = "/shop_register_by_phone", method = POST)
    public Object findShopRegisterByPhone(@RequestParam("shopPhone") String shopPhone) {

        return shopRegisterBussiness.findShopRegisterByShopId(shopPhone);
    }
}
